package dbexp.model;

import static dbexp.model.db.DbOT.PACKAGE;
import static dbexp.model.db.DbOT.PACKAGES;

import java.util.ArrayList;

public class Package extends SourceType {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	public static final String LABEL = "pakiety";
	public static final String DEF_ICON = "package-icon";

	public Package() {
		super(LABEL, PACKAGES);
		setIcons(DEF_ICON);
	}

	public Package(String name) {
		super(name, PACKAGE);
		setIcons(DEF_ICON);
		set("leaf", true);
	}

	public Package(String name, String schema) {
		this(name);
		setSchema(schema);
	}

	public static Package instantOf(SourceType st) {
		Package pack = new Package(st.getName(), st.getSchema());
		pack.setErrors(st.getErrors());
		pack.setLinies(st.getLinie());
		pack.setWrapped(st.isWrapped());
		return pack;
	}

	public String getName() {
		return super.getName();
	}

	public String getSchema() {
		return super.getSchema();
	}

	public ArrayList<String> getLinie() {
		return super.getLinie();
	}

	public ArrayList<Error> getErrors() {
		return super.getErrors();
	}

	public String getErrorString() {
		String out = " ";
		for (int i = 0; i < getBledy().size(); i++) {
			out += getErrors().get(i).getText();
		}
		return out;
	}

	public String getContent() {
		String out = " ";
		for (int i = 0; i < getLinie().size(); i++) {
			out += getLine(i);
		}
		return out;
	}

	private String getQuery(String oper) {
		if (requiredFieldIsNotNull()) {
			String query = "PACKAGE " + getObjectNameForQuery();
			return oper + "\n" + query + "\n" + getContent();
		} else {
			return oneFromDual();
		}
	}

	public String getQuery(int operation) {
		if (operation == CREATE) {
			return getQuery("CREATE OR REPLACE ");
		} else if (operation == ALTER) {
			return "ALTER PACKAGE " + getObjectNameForQuery() + " COMPILE";
		} else if (operation == DELETE) {
			return "DROP PACKAGE " + getObjectNameForQuery();
		}
		return oneFromDual();
	}
}
